package com.dji.compose_learning.ui.view

import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

@Composable
fun SimpleColumnList() {
    Surface(
        modifier = Modifier.fillMaxSize(),
        color = MaterialTheme.colorScheme.background
    ) {
        Row(modifier = Modifier.fillMaxSize()) {
            val scope = rememberCoroutineScope()

            val scallable = rememberScrollState()
            Column {
                Row {
                    Button(onClick = {
                        scope.launch {
                            scallable.animateScrollTo(0)
                        }
                    }) {
                        Text(text = "Scorll to top")
                    }
                    Spacer(modifier = Modifier.width(20.dp))
                    Button(onClick = {
                        scope.launch {
                            scallable.animateScrollTo(scallable.maxValue)
                        }
                    }) {
                        Text(text = "Scroll to bottom")
                    }
                }
                Column(modifier = Modifier
                    .fillMaxWidth(0.5f)
                    .fillMaxHeight()
                    .verticalScroll(scallable)  //添加滚动，否则无法滚动
                    .padding(16.dp)) {
                    repeat(100) {
                        Text(text = "simple item$it",
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(10.dp),
                            textAlign = TextAlign.Center)
                    }
                }
            }

            val scrollState = rememberLazyListState() //滚动状态
            Column {
                Row {
                    Button(onClick = {
                        scope.launch {
                            scrollState.animateScrollToItem(0)
                        }
                    }) {
                        Text(text = "Scorll to top")
                    }
                    Spacer(modifier = Modifier.width(20.dp))
                    Button(onClick = {
                        scope.launch {
                            scrollState.animateScrollToItem(99)
                        }
                    }) {
                        Text(text = "Scroll to bottom")
                    }
                }
                LazyColumn(state = scrollState) {
                    items(100) {
                        Text(text = "lazy item$it",
                            modifier = Modifier
                                .fillMaxWidth()
                                .padding(10.dp),
                            textAlign = TextAlign.Center)
                    }
                }
            }
        }
    }
}